# url：http://www.bjhospital.net/
# 用例1：
# 搜索骨科—>点击搜索—>点击出来的第一个链接—>断言文本内容
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
class SearchCase:
    def __init__(self,driver):
        self.driver = driver
        self.driver.implicitly_wait(15)
    def open_url(self,url):
        self.driver.get(url)
        self.driver.maximize_window()
    def input_text(self,text):
        self.driver.find_element(By.CLASS_NAME,"keyword").send_keys(text)
    def click_search_button(self):
        self.driver.find_elements(By.CLASS_NAME,"btn")[0].click()
        time.sleep(2) # 等待刷到最新的页面
    def click_one(self):
        # self.driver.find_element(By.XPATH,"/html/body/div[4]/ul/li[1]/div[1]/h5/a").click()
        """
        先定位到父节点
        通过父节点定位到子节点
        :return:
        """
        resultWrap = self.driver.find_element(By.CLASS_NAME,"resultWrap")
        ul = resultWrap.find_element(By.TAG_NAME,"ul")
        li = ul.find_elements(By.TAG_NAME,'li')[0]
        h5 = li.find_element(By.TAG_NAME,"h5")
        h5.find_element(By.TAG_NAME,"a").click()
    def windows_handles1(self):
        winds = self.driver.window_handles
        self.driver.switch_to.window(winds[-1])
    def assert_text(self):
        text = self.driver.find_element(By.XPATH,"/html/body/div[3]/div/div/article/h1").text
        assert text == "毕节市第一人民医院高值医用耗材网上阳光平台议价意向公开"
    def quit_driver(self):
        self.driver.quit()

if __name__ == '__main__':
    driver = webdriver.Chrome()
    s1 = SearchCase(driver)
    s1.open_url("http://www.bjhospital.net/")
    s1.input_text("骨科")
    s1.click_search_button()
    s1.click_one()
    s1.windows_handles1()
    s1.assert_text()
    s1.quit_driver()


